package com.gict.antifraudback.utils.magnet;

import com.gict.antifraudback.entity.MagnetFile;
import com.gict.antifraudback.utils.JsoupUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * bt联盟解析
 */
@Service
public class BTLMMagnet implements IMagnet {

    public static final String source = "BT联盟";

    private static final CssQuery CSS_QUERY = new CssQuery();

    static {
        CSS_QUERY.setSource(source);
        CSS_QUERY.setNameCssQuery(".list-title");
        CSS_QUERY.setLinkCssQuery("a.common-link");
        CSS_QUERY.setUlCssQuery("ul.list-unstyled");
        CSS_QUERY.setSizeCssQuery(".result-resource-meta-info-value");
    }

    private String getSearchUrl(String keyword) {
        return String.format("https://cdn.btlm.top:65533/s?word=%s", keyword);
    }

    @Override
    public String getSource() {
        return source;
    }

    @Override
    public MagnetFile getMagnetByKeyWord(String keyword) {
        Document doc = JsoupUtil.sendGet(getSearchUrl(keyword));
        if (doc != null) {
            List<MagnetFile> magnetFileList = this.parseAll(doc, CSS_QUERY);
            if (!CollectionUtils.isEmpty(magnetFileList)) {
                return magnetFileList.get(0);
            }
        }
        return null;
    }

    @Override
    public void parseItem(Elements elements, List<MagnetFile> magnetFileList) {
        // 遍历所有的 <ul> 元素
        for (Element ul : elements) {
            MagnetFile magnetFile = new MagnetFile();
            magnetFile.setSource(source);
            magnetFile.setDescription("");
            magnetFile.setImageLink("");

            // 处理每个 <li> 元素的内容
            Element element = ul.selectFirst(CSS_QUERY.getNameCssQuery());
            if (element != null) {
                magnetFile.setName(element.text());
            }
            // 设置链接
            Element link = ul.selectFirst(CSS_QUERY.getLinkCssQuery());
            if (link != null) {
                magnetFile.setMagnetLink(parseLinkUrl(link.attr("href")));
            }

            // 设置大小
            Element fileSize = ul.selectFirst(CSS_QUERY.getSizeCssQuery());
            if (fileSize != null) {
                magnetFile.setFileSize(parseSize(fileSize.text()));
            }
            magnetFileList.add(magnetFile);
        }
    }

    private String parseLinkUrl(String link) {
        return String.format("magnet:?xt=urn:btih:%s", link.replace("/hash/", ""));
    }

    //https://cdn.btlm.top:65533/s?word=fc2&page=2
    @Override
    public List<MagnetFile> listMagnetByKeyWord(String keyword, Integer page) {
        Document doc = JsoupUtil.sendGet(getSearchUrl(keyword) + (page == null ? "" : ("&page=" + page)));
        if (doc != null) {
            List<MagnetFile> magnetFileList = this.parseAll(doc, CSS_QUERY);
            saveToDb(magnetFileList);
            return magnetFileList;
        }
        return null;
    }
}
